import * as api from '@/api/role';

const state = {
  // 角色列表
  list: {
    rows: [],
    total: 0,
  },
  // 所有权限
  permissionList: [],
  // 当前角色权限
  permission: [],
};

const mutations = {
  SET_LIST (state, newValue) {
    state.list = newValue;
  },
  SET_PERMISSION_LIST (state, newValue) {
    state.permissionList = newValue;
  },
  SET_PERMISSION (state, newValue) {
    state.permission = newValue;
  },
};

const actions = {
  async updateList ({ commit }, params) {
    const res = await api.getList(params);
    commit('SET_LIST', res);
  },
  async updatePermissionList ({ commit }) {
    const res = await api.getPermissionList();
    commit('SET_PERMISSION_LIST', res);
  },
  async updatePermission ({ commit }, id) {
    const res = await api.getPermission(id);
    commit('SET_PERMISSION', res);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}